/*
 * Copyright (c) 2024 Your Company
 * Licensed under the Apache License, Version 2.0
 */

#ifndef FOUNDATION_ONVIF_DEVICE_SERVICE_HPP
#define FOUNDATION_ONVIF_DEVICE_SERVICE_HPP

#include <string>
#include <vector>
#include <memory>
#include <functional>
#include "onvif_types.h"
#include "soap/soap_message.h"

namespace El {
namespace Onvif {

/**
 * @brief ONVIF设备管理服务实现
 * 
 * 实现ONVIF Device Management Service规范，提供设备信息查询、
 * 系统时间管理、网络配置、用户管理等功能
 */
class DeviceService : public ISoapHandler {
public:
    DeviceService();
    virtual ~DeviceService() = default;

    // ISoapHandler接口实现
    std::string HandleRequest(const SoapMessage& request, const std::string& remoteAddr) override;

    // 设备信息管理
    void SetDeviceInformation(const DeviceInformation& info);
    DeviceInformation GetDeviceInformation() const;
    
    // 系统时间管理
    void SetSystemDateTime(const SystemDateTime& dateTime);
    SystemDateTime GetSystemDateTime() const;
    
    // 网络配置管理
    void SetNetworkConfiguration(const NetworkInterface& config);
    NetworkInterface GetNetworkConfiguration() const;
    
    // 用户管理
    void AddUser(const User& user);
    void RemoveUser(const std::string& username);
    std::vector<User> GetUsers() const;
    
    // 系统时间更新
    void UpdateSystemDateTime();
    
    // 服务能力查询
    void SetServiceCapabilities(const ServiceCapabilities& capabilities);
    ServiceCapabilities GetServiceCapabilities() const;
    
    // 作用域管理
    void SetScopes(const std::vector<Scope>& scopes);
    std::vector<Scope> GetScopes() const;
    
    // 发现URL设置
    void SetDiscoveryMode(bool enabled);
    void SetXAddrs(const std::vector<std::string>& xaddrs);
    
    // WS-Discovery相关
    void SetProbeMatches(const std::vector<std::string>& types,
                        const std::vector<Scope>& scopes);

    // 配置变更回调
    using ConfigChangeCallback = std::function<void(const std::string& configType)>;
    void SetConfigChangeCallback(ConfigChangeCallback callback);

private:
    // 具体的SOAP处理方法
    std::string HandleGetDeviceInformation(const SoapMessage& message);
    std::string HandleGetSystemDateAndTime(const SoapMessage& message);
    std::string HandleSetSystemDateAndTime(const SoapMessage& message);
    std::string HandleGetNetworkInterfaces(const SoapMessage& message);
    std::string HandleSetNetworkInterfaces(const SoapMessage& message);
    std::string HandleCreateUsers(const SoapMessage& message);
    std::string HandleDeleteUsers(const SoapMessage& message);
    std::string HandleGetUsers(const SoapMessage& message);
    std::string HandleGetCapabilities(const SoapMessage& message);
    std::string HandleGetScopes(const SoapMessage& message);
    std::string HandleSetScopes(const SoapMessage& message);
    std::string HandleGetServices(const SoapMessage& message);
    std::string HandleSystemReboot(const SoapMessage& message);
    std::string HandleGetHostname(const SoapMessage& message);
    std::string HandleSetHostname(const SoapMessage& message);
    std::string HandleGetDNS(const SoapMessage& message);
    std::string HandleSetDNS(const SoapMessage& message);
    std::string HandleGetNTP(const SoapMessage& message);
    std::string HandleSetNTP(const SoapMessage& message);
    std::string HandleGetNetworkProtocols(const SoapMessage& message);
    std::string HandleSetNetworkProtocols(const SoapMessage& message);
    std::string HandleRestoreSystem(const SoapMessage& message);

    // 响应生成辅助方法
    std::string GenerateDeviceInformationResponse(const DeviceInformation& info);
    std::string GenerateSystemDateTimeResponse(const SystemDateTime& dateTime);
    std::string GenerateNetworkInterfacesResponse(const std::vector<NetworkInterface>& interfaces);
    std::string GenerateUsersResponse(const std::vector<User>& users);
    std::string GenerateCapabilitiesResponse(const ServiceCapabilities& capabilities);
    std::string GenerateScopesResponse(const std::vector<Scope>& scopes);
    std::string GenerateServicesResponse();
    std::string GenerateRebootResponse();
    
    // XML转义
    std::string EscapeXml(const std::string& input) const;
    
    // 网络配置更新
    void UpdateNetworkConfiguration();
    
    // 获取当前IP地址
    std::string GetCurrentIpAddress() const;
    
    // 计算前缀长度
    int CalculatePrefixLength(const std::string& netmask) const;
    
    // 服务端点URL生成
    std::string GetServiceEndpointUrl(const std::string& service) const;

private:
    mutable std::mutex deviceMutex_;
    mutable std::mutex systemMutex_;
    mutable std::mutex usersMutex_;
    
    DeviceInformation deviceInfo_;
    SystemDateTime systemDateTime_;
    NetworkInterface networkConfig_;
    std::vector<User> users_;
    ServiceCapabilities serviceCapabilities_;
    std::vector<Scope> scopes_;
    std::vector<std::string> xaddrs_;
    bool discoveryEnabled_;
    
    ConfigChangeCallback configChangeCallback_;
    
    // 当前IP地址缓存
    mutable std::string currentIpAddress_;
    
    // 主机名
    std::string hostname_;
    
    // 网络协议配置
    struct NetworkProtocol {
        std::string name;
        bool enabled;
        int port;
    };
    std::vector<NetworkProtocol> networkProtocols_;
    
    // DNS配置
    struct DNSInformation {
        bool fromDHCP;
        std::vector<std::string> searchDomain;
        std::vector<std::string> dnsFromDHCP;
        std::vector<std::string> dnsManual;
    };
    DNSInformation dnsInfo_;
    
    // NTP配置
    struct NTPInformation {
        bool fromDHCP;
        std::vector<std::string> ntpFromDHCP;
        std::vector<std::string> ntpManual;
    };
    NTPInformation ntpInfo_;
    
    // 网络网关配置
    struct NetworkGateway {
        std::vector<std::string> ipv4Address;
        std::vector<std::string> ipv6Address;
    };
    NetworkGateway networkGateway_;
    
    // 固件升级状态
    enum class UpgradeStatus {
        NotStarted,
        InProgress,
        Completed,
        Failed
    };
    UpgradeStatus upgradeStatus_;
    
    // 系统还原状态
    enum class RestoreStatus {
        NotStarted,
        InProgress,
        Completed,
        Failed
    };
    RestoreStatus restoreStatus_;
};

} // namespace Onvif
} // namespace El

#endif // FOUNDATION_ONVIF_DEVICE_SERVICE_HPP 